

$(document).ready(function() {
    /*查询我的申请*/
    query();

});


/*查询我的申请*/
var query=function () {
    $('#dataTable').dataTable({
        "bDeferRender":true,
        "sAjaxDataProp":"data.dorStuTermList",
        "sAjaxSource":"http://localhost:8080/dormitory/queryMyApplys?stuNum="+20151000,
        "sPaginationType": "bs_full", //"bs_normal", "bs_two_button", "bs_four_button", "bs_full"
        "fnPreDrawCallback": function( oSettings ) {
            $('.dataTables_filter input').addClass('form-control input-large').attr('placeholder', 'Search');
            $('.dataTables_length select').addClass('form-control input-small');
        },
        "oLanguage": {
            "sSearch": "",
            "sLengthMenu": "<span>_MENU_ entries</span>"
        },
        "bJQueryUI": false,
        "bAutoWidth": false,
        "sDom": "<'row'<'col-lg-6 col-md-6 col-sm-12 text-center'l><'col-lg-6 col-md-6 col-sm-12 text-center'f>r>t<'row-'<'col-lg-6 col-md-6 col-sm-12'i><'col-lg-6 col-md-6 col-sm-12'p>>",

        "aoColumns": [
            {"mDataProp":"stuNum"},
            {"mDataProp":"stuName"},
            {"mDataProp":"stuSex",
                "mRender": function(data) {
                    var value = "男";
                    if (data==1){
                        value = "女";
                    }
                    return value;
                }
            },
            {"mDataProp":"academyName"},
            {"mDataProp":"termYear"},
            {"mDataProp":"termNum"},
            {"mDataProp":"dormitoryNum"},
            {"mDataProp":"applyDate"},
            {"mDataProp":"applyReason"},
            {"mDataProp":"startDate"},
            {"mDataProp":"endDate"},
            {"mDataProp":"applyState",
                "mRender": function(data) {
                    var value = "<span class='label label-danger mr10 mb10'>未通过</span>";
                    if (data==1){
                        value = "<a href='#' title='点击下载留宿证'><span id='download' class='label label-success mr10 mb10'>已通过</span></a>";
                    }if (data==2){
                        value = "<span class='label label-yellow mr10 mb10'>审核中</span>";
                    }
                    return value;
                }
            }
        ],

    })
};



$(document).on('click', '#download', function() {
    var $stuName=$(this).parents("tr").children(0).eq(1).text();
    var $stuSex=$(this).parents("tr").children(0).eq(2).text();
    var $dormitoryNum=$(this).parents("tr").children(0).eq(6).text();
    var $startDate=$(this).parents("tr").children(0).eq(9).text();
    var $endDate=$(this).parents("tr").children(0).eq(10).text();

     $.ajax({
         url: "http://localhost:8080/dormitory/download",
         type: "GET",
         async: true,
         data: "stuName=" + $stuName+"&stuSex="+$stuSex+"&dormitoryNum="+$dormitoryNum+"&startDate="+$startDate+"&endDate="+$endDate,
         dataType: "json",
         success:function () {
             layer.alert("留宿证已成功保存到桌面！");
         },
         error:function () {
             layer.alert("保存留宿证失败！请重新尝试！");
         }
     });




});


/*查询我的申请*/
/*$(document).on('click', '#queryDormitory-btn', function() {
    $.ajax({
        url:"http://localhost:8080/dormitory/queryMyApplys",
        type:"GET",
        data:"stuNum="+20151000,
        dataType:"json",
        success:function (data) {
            console.log(data.data);
            /!* var $tbody=$("#dormitoryFormTable");
             $tbody.html("");
             var $tr_array="";
             console.log(data.data);
             var $input="";
              $.each(data.data.dorStuTermList,function (index, value) {
                  console.log(data.data.dorStuTermList)
                  $tr_array+="<tr class='gradeA'><td>"+value.stuNum+"</td>><td>"+value.stuName+"</td><td>"+value.stuSex+"</td><td>"
                      +value.academyName+"</td><td>"+value.termYear+" "+value.termNum+"</td><td>"+value.dormitoryNum+"</td><td>"
                      +value.applyDate+ "</td><td>" +value.startDate+"</td><td>"+value.endDate+"</td>";
                  if(value.applyState==1){
                      $tr_array+="<td>已通过</td></tr>"
                  }else {
                      $tr_array+="<td>未通过</td></tr></table>"
                  }
              });
              $tbody.append($tr_array);*!/
        }
    })
});*/

/*提交申请*/
$(document).on('click', '#apply-btn', function() {
    // var $stuName=$("input[name='stuName']").val();
    //var $sutNum=$("input[name='sutNum']").val();
    var $stuNum=20151000;
    // var $stuSex=$("input[name='stuSex']").val();
    // var $dormitoryNum=$("input[name='dormitoryNum']").val();
    // var $termYear=$("input[name='termYear']").val();
    // var $termNum=$("input[name='termNum']").val();
    var $termYear="2015-2016";
    var $termNum="第一学期";
    var $startDate=$("input[name='startDate']").val();
    var $endDate=$("input[name='endDate']").val();
    var $reason=$("textarea[name='reason']").val();


    $.ajax({

        url:"http://localhost:8080/dormitory/applyDor",
        type:"POST",
        data:"stuNum="+$stuNum+"&termYear="+$termYear+
             "&termNum="+$termNum+"&startDate="+$startDate+"&endDate="+$endDate+"&reason="+$reason,
        dataType:"json",
        success:function (data,status) {
            console.log(data.data)
            layer.alert("申请已提交，请等待老师批复。")
        },
        error:function (data) {
            layer.alert("申请失败，请尝试重试。")
        }
    })
});


$(document).on('click','#re-btn',function () {
    $("input[name='startDate']").val("");
    $("input[name='endDate']").val("");
    $("textarea[name='reason']").val("");

});
